package com.zpark.resume.controller;

import com.zpark.resume.common.ResponseResult;
import com.zpark.resume.common.StatusCode;
import com.zpark.resume.entity.Cv;
import com.zpark.resume.entity.Member;
import com.zpark.resume.entity.Recommend;
import com.zpark.resume.service.IRecommendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @PackageName: com.zpark.resume.controller
 * @ClassName: RecommendController
 * @Description:个人推荐信控制层接口
 * @author: RZS
 * @date: 2023/6/1  17:40
 */
@RestController
@CrossOrigin
@RequestMapping("/recommend")
public class RecommendController {

    @Autowired
    private IRecommendService recommendService;

    /**
     * 根据member_id查询recommend数据
     * @param id member_id
     * @return
     */
    @PostMapping("/find_by_member_id/{id}")
    public ResponseResult<Recommend> findRecommendByMemberId(@PathVariable("id") Integer id){
        // 调用业务层接口查询数据
        Recommend recommend = recommendService.findByMemberId(id);
        return new ResponseResult<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), recommend);
    }

    /**
     * 根据member_id查询recommend数据
     * @return
     */
    @GetMapping("/find_member_id")
    public ResponseResult<Recommend> findRecommendMemberId(HttpSession session){
        // 从session对象当中获取当前登录会员对象
        Member member = (Member) session.getAttribute("member");
        // 调用业务层接口查询数据
        Recommend recommend = recommendService.findByMemberId(member.getId());
        return new ResponseResult<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), recommend);
    }

    /**
     * 添加自荐信
     * @return
     */
    @PostMapping("/add")
    public ResponseResult<Recommend> addRec(@RequestBody Recommend recommend, HttpSession session){
        // 从session对象当中获取当前登录会员对象
        Member member = (Member) session.getAttribute("member");
        // 调用业务层接口添加数据
        recommendService.addRecommend(recommend, member.getId());
        return new ResponseResult<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), null);
    }

    /**
     * 修改自荐信
     * @return
     */
    @PostMapping("/update")
    public ResponseResult<Recommend> changeRec(@RequestBody Recommend recommend){
        // 调用业务层接口添加数据
        recommendService.changeRecommend(recommend);
        return new ResponseResult<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), null);
    }


}
